package com.liu.webstore.aspect;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestHolder {

    private static final Logger                                    logger                                         = LoggerFactory
            .getLogger(RequestHolder.class);

    public static final String                                     GET                                            = "GET";
    public static final String                                     POST                                           = "POST";

    public static final String                                     UTF8                                           = "UTF-8";

    public static InheritableThreadLocal<ServletRequestAttributes> servletRequestAttributesInheritableThreadLocal = new InheritableThreadLocal();

    public static HttpServletRequest getRequest() {
        return getServletRequestAttributes().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return getServletRequestAttributes().getResponse();
    }

    public static ServletRequestAttributes setServletRequestAttributes(ServletRequestAttributes servletRequestAttributes) {
        servletRequestAttributesInheritableThreadLocal.set(servletRequestAttributes);
        return servletRequestAttributes;
    }

    public static void removeServletRequestAttributes() {
        servletRequestAttributesInheritableThreadLocal.remove();
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (requestAttributes == null) {
            requestAttributes = servletRequestAttributesInheritableThreadLocal.get();
        }
        return requestAttributes;
    }
}
